import Vue from 'vue'
import {SERVER_URL} from '../config'
import http from './http'
import { WechatPlugin } from 'vux'
Vue.use(WechatPlugin)

// 微信分享
export const share = (obj, callback) => {
  let url = () => {
    return window.location.href.split('#')[0]
  }
  if (!obj) {
    alert('请传入分享参数')
    return false
  }
  let title = obj.title === undefined || obj.title === null ? '天南山文化' : obj.title
  let link = obj.link === undefined || obj.link === null ? url() : obj.link
  let desc = obj.desc === undefined || obj.desc === null ? '天南山文化' : obj.desc
  let imgUrl = obj.imgUrl === undefined || obj.imgUrl === null ? './logo.png' : obj.imgUrl

  http.get(SERVER_URL + '/api/wechat/jssdk').then((response) => {
    Vue.wechat.config(response.data)
    Vue.wechat.ready(function () {
      // 分享到朋友圈
      Vue.wechat.onMenuShareTimeline({
        title: title,
        link: link,
        desc: desc,
        imgUrl: imgUrl,
        success: function () {
          callback && callback()
        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      })
      // 分享到朋友
      Vue.wechat.onMenuShareAppMessage({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
        type: '',
        dataUrl: '',
        success: function () {
          callback && callback()
        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      })
      // 分享到QQ
      Vue.wechat.onMenuShareQQ({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
        success: function () {
          callback && callback()
        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      })
      // 分享到QQ空间
      Vue.wechat.onMenuShareQZone({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
        success: function () {
          callback && callback()
        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      })
      // 分享到腾讯微博
      Vue.wechat.onMenuShareWeibo({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
        success: function () {
          callback && callback()
        },
        cancel: function () {
          // 用户取消分享后执行的回调函数
        }
      })
    })
  })
}
